<?php
namespace App\Http\Controllers\Admin;

use App\Services\Admin\AdminService;
use Illuminate\Http\Request;

class LoginController extends BaseController
{
    // 登录

    public function __construct(AdminService $adminService)
    {
        $this->db = $adminService;
        $this->cors();
    }

    // 账号登录
    public function dologin(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        //数据验证
        $this->validate($request, [
            'uname' => 'required|min:2|max:255',
            'passwd' => 'required|min:6|max:20',
        ], [
            'uname.required' => '账号是必填的',
            'uname.min' => '账号不能少于2位',
            'uname.max' => '账号不能多于255位',
            'passwd.required' => '密码是必填的',
            'passwd.min' => '密码不能少于6位',
            'passwd.max' => '密码不能多于20位',
        ]);
        $uname = $request->post('uname');
        $passwd = $request->post('passwd');
        $rst = $this->db->doLogin($uname, $passwd);
        if ($rst['code'] != 0) {
            return $this->toJson(-2, $rst['msg']);
        }
        $mdid = md5($rst['data']['id']);
        //返回数据
        $resData = [
            'aid' => $rst['data']['id'],
            'adminName' => $rst['data']['name'],
            'lastip' => $rst['data']['lastip'],
            'lasttime' => $rst['data']['lasttime'],
        ];
        return $this->toJson(0, '', $resData);
    }
}